package com.objectproject.main.service.subsystem;

import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.objectproject.main.dao.MyGroupDAOImpl;
import com.objectproject.main.dao.MyObjectDAOImpl;
import com.objectproject.main.model.MyObject;
import com.objectproject.main.model.MyObjectCommandModel;
import com.objectproject.main.model.MyProperty;

@Service
public class MyObjectCommandModelExtractor {
	
	@Autowired
	MyGroupDAOImpl myGroupDAOImpl;
	
	@Autowired
	MyObjectDAOImpl myObjectDAOImpl;
	
	public MyObject extractFromCommandModel(MyObjectCommandModel myObjectCommandModel){
		
		MyObject myObject = new MyObject();
		ArrayList<MyProperty> myProperties = new ArrayList<MyProperty>();
		
		myObject.setModificationAuth(myGroupDAOImpl.selectMyGroupById(myObjectCommandModel.getObjectModificationAuthId()));
		myObject.setInheritanceAuth(myGroupDAOImpl.selectMyGroupById(myObjectCommandModel.getObjectInheritanceAuthId()));
		
		myObject.setAnonymous(myObjectCommandModel.isAnonymous());
		myObject.setFinal(myObjectCommandModel.isFinal());
		myObject.setInterface(myObjectCommandModel.isInterface());
		
		for(int i = 1 ; i < myObjectCommandModel.getPropertySize() + 1 ; i++){
			
			MyProperty myProperty = new MyProperty();
			
			myProperty.setPropertyId(myObjectCommandModel.getPropertyIds().get(i));
			myProperty.setHidden(new Boolean(myObjectCommandModel.getIsHiddens().get(i)));
			myProperty.setNext(new Boolean(myObjectCommandModel.getIsNexts().get(i)));
			myProperty.setStatic(new Boolean(myObjectCommandModel.getIsStatics().get(i)));
			myProperty.setSystemProperty(new Boolean(myObjectCommandModel.getIsSystemProperties().get(i)));
			
			if(myObjectCommandModel.getPropertyModificationAuthIds().get(i) != 0L){
				myProperty.setModificationAuth(myGroupDAOImpl.selectMyGroupById(myObjectCommandModel.getPropertyModificationAuthIds().get(i)));
			}
			
			myProperty.setTypeOfValue(myObjectCommandModel.getValueTypes().get(i));
			
			if(myObjectCommandModel.getMyObjectQualifiers().get(i) != 0L){
				myProperty.setTypeObjectQualifier(myObjectDAOImpl.selectMyObjectById(myObjectCommandModel.getMyObjectQualifiers().get(i)));
			}
			
			myProperty.setViewMini(new Boolean(myObjectCommandModel.getViewMinis().get(i)));
			myProperty.setViewSimple(new Boolean(myObjectCommandModel.getViewSimples().get(i)));
			myProperty.setViewCard(new Boolean(myObjectCommandModel.getViewCards().get(i)));
			myProperty.setViewNormal(new Boolean(myObjectCommandModel.getViewNormals().get(i)));
			
			myProperty.setLabel(myObjectCommandModel.getLabels().get(i));
			myProperty.setValueId(myObjectCommandModel.getValueIds().get(i));
			myProperty.setHelpText(myObjectCommandModel.getHelpTexts().get(i));
			
			myProperties.add(myProperty);
			
		}
		
		myObject.setMyProperties(myProperties);
		
		return myObject;
		
	}
	
}
